package com.haoshuang.zhouzhoubang.services

import android.content.Context
import android.content.Intent
import android.content.res.AssetManager
import android.media.MediaPlayer
import android.util.Log
import cn.jpush.android.api.CmdMessage
import cn.jpush.android.api.CustomMessage
import cn.jpush.android.api.JPushInterface
import cn.jpush.android.api.JPushMessage
import cn.jpush.android.api.NotificationMessage
import cn.jpush.android.service.JPushMessageService
import com.alibaba.fastjson.JSONObject
import com.haoshuang.zhouzhoubang.config.Config
import com.haoshuang.zhouzhoubang.model.event.RefreshHomeOrderEvent
import com.haoshuang.zhouzhoubang.ui.page.main.MainActivity
import com.haoshuang.zhouzhoubang.utils.MediaPlayUtils
import com.haoshuang.zhouzhoubang.utils.TagAliasOperatorHelper
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lnkj.jialubao.utils.AccountUtils.assignOrderNotice
import com.lnkj.jialubao.utils.AccountUtils.errorOrderNotice
import com.lnkj.jialubao.utils.AccountUtils.finishOrderNotice
import com.lnkj.jialubao.utils.AccountUtils.newOrderNotice
import com.lnkj.jialubao.utils.AccountUtils.timeoutOrderNotice
import com.tencent.mmkv.MMKV
import java.io.IOException

class MyPushService: JPushMessageService() {

    private val TAG = "PushMessageReceiver"

    override fun onMessage(context: Context?, customMessage: CustomMessage) {
        Log.e(TAG, "[onMessage] $customMessage")
    }

    override fun onNotifyMessageOpened(context: Context, message: NotificationMessage) {
        Log.e(TAG, "[onNotifyMessageOpened] $message")
        val extras = message.notificationExtras
        val jsonObject = JSONObject.parseObject(extras)
        val type = jsonObject.getString("type")
        val orderId = jsonObject.getString("orderId")
        when (type) {
            "1", "10" -> {
                // 新订单
                val intent = Intent(context, MainActivity::class.java)
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
                context.startActivity(intent)
            }

            "2", "3", "4", "5", "6", "7" -> {
                val intent = Intent(context, MainActivity::class.java)
                intent.putExtra("index", "2-2")
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
                context.startActivity(intent)
            }

            "8", "9" -> {
                val intent = Intent(context, MainActivity::class.java)
                intent.putExtra("index", "2-3")
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                context.startActivity(intent)
            }

            else -> {
                val i = Intent(context, MainActivity::class.java)
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
                context.startActivity(i)
            }
        }
    }

    override fun onMultiActionClicked(context: Context?, intent: Intent) {
        Log.e(TAG, "[onMultiActionClicked] 用户点击了通知栏按钮")
        val nActionExtra = intent.extras!!.getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA)

        //开发者根据不同 Action 携带的 extra 字段来分配不同的动作。
        if (nActionExtra == null) {
            Log.d(TAG, "ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null")
            return
        }
        if (nActionExtra == "my_extra1") {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮一")
        } else if (nActionExtra == "my_extra2") {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮二")
        } else if (nActionExtra == "my_extra3") {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮三")
        } else {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮未定义")
        }
    }

    override fun onNotifyMessageArrived(context: Context, message: NotificationMessage) {
        Log.e(TAG, "[onNotifyMessageArrived] $message")
        /*
        1-新订单
2-指派
3-已确认上门
4-待服务
5-已验收
6-距离服务结束还有15分钟
7-订单距离超时仅剩
8-异常订单
9-返工单
         */
        val str = MMKV.defaultMMKV().decodeString("voice_prompt", "1")
        val extras = message.notificationExtras
        val jsonObject = JSONObject.parseObject(extras)
        val type = jsonObject.getString("type")
        val orderId = jsonObject.getString("orderId")
        Log.e("TAG", "type: $type, orderId: $orderId")
        if (str == "1") {
            var path = ""
            when (type) {
                "1" -> {
                    // 新订单
                    path = newOrderNotice
                    LiveEventBus.get(RefreshHomeOrderEvent::class.java)
                        .post(RefreshHomeOrderEvent())
                }

                "10" -> {
                    path = "new_order_money.mp3"
                    LiveEventBus.get(RefreshHomeOrderEvent::class.java)
                        .post(RefreshHomeOrderEvent())
                }

                "2" ->                     // 指派
                    path = assignOrderNotice

                "6" ->                     // 距离服务结束还有15分钟
                    path = finishOrderNotice

                "7" ->                     // 订单上门超时
                    path = timeoutOrderNotice

                "8", "9" -> path = errorOrderNotice
                else -> path = Config.defaultNotice
            }
            MediaPlayUtils.playAudio(path)
        }
    }

    override fun onNotifyMessageDismiss(context: Context?, message: NotificationMessage) {
        Log.e(TAG, "[onNotifyMessageDismiss] $message")
    }

    override fun onRegister(context: Context, registrationId: String) {
        Log.e(TAG, "[onRegister] $registrationId")
        val intent = Intent("com.jiguang.demo.register")
        context.sendBroadcast(intent)
    }

    override fun onConnected(context: Context?, isConnected: Boolean) {
        Log.e(TAG, "[onConnected] $isConnected")
    }

    override fun onCommandResult(context: Context?, cmdMessage: CmdMessage) {
        Log.e(TAG, "[onCommandResult] $cmdMessage")
    }

    override fun onTagOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
        TagAliasOperatorHelper.getInstance().onTagOperatorResult(context, jPushMessage)
        super.onTagOperatorResult(context, jPushMessage)
    }

    override fun onCheckTagOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
        TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context, jPushMessage)
        super.onCheckTagOperatorResult(context, jPushMessage)
    }

    override fun onAliasOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
        TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context, jPushMessage)
        super.onAliasOperatorResult(context, jPushMessage)
    }

    override fun onMobileNumberOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
        TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context, jPushMessage)
        super.onMobileNumberOperatorResult(context, jPushMessage)
    }

    override fun onNotificationSettingsCheck(context: Context?, isOn: Boolean, source: Int) {
        super.onNotificationSettingsCheck(context, isOn, source)
        Log.e(TAG, "[onNotificationSettingsCheck] isOn:$isOn,source:$source")
    }


}